package com.celesea.sso.configuration.web;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author JackYuan
 * @create 2017-11-01 10:15
 */

@ControllerAdvice
public class ExceptionAdvice {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @ExceptionHandler({Throwable.class})
    @ResponseBody
    public Object handlerException(
            HttpServletRequest request,
            HttpServletResponse response,
            HandlerMethod handlerMethod,
            Throwable throwable) throws Throwable {
        // fetch root Exception wraped by HystrixRuntimeException
        logger.error("server exception：", throwable);
        return throwable.getMessage();
    }
}
